/**
 * Created by HEKI on 8/25/2016.
 */
angular.module("config")
    .controller("registController", function ($scope,$http,$location,md5) {

        $scope.error = {};
        console.log("In this Controller");
        $scope.userRegist = function () {
            $scope.error = {};
            if($scope.cPersonName && $scope.cTel && $scope.cPassword1 && $scope.cLocation){
                if($scope.cPassword1==$scope.cPassword2){
                    var req = {
                        method: "post",
                        url: "/client",
                        data:{
                            cPersonName:$scope.cPersonName,
                            cTel:$scope.cTel,
                            cPassword:md5.createHash($scope.cPassword1 || ""),
                            cLocation:$scope.cLocation
                        }
                    }

                    $http(req)
                        .then(function (res) {
                            console.log(res.data.error)
                            if (!res.data.error) {
                                $scope.$emit('UserRegist', $scope.cPersonName);
                                $location.path("#/")
                            }
                            else {
                                if(res.data.error == "电话已注册!")
                                    $scope.error.telNotNull = "电话已注册";
                            }
                        });


                }
                else{
                    $scope.error.passwordTesting = "两次密码不匹配";
                }
            }
            else{
                if(!$scope.cPersonName){
                    $scope.error.personNameNotNull = "昵称不能为空";
                }
                else if(!$scope.cTel){
                    $scope.error.telNotNull = "电话不能为空";
                }
                else if(!$scope.cPassword1){
                    $scope.error.passwordNotNull = "密码不能为空";
                }
                else if(!$scope.cLocation){
                    $scope.error.LocationNotNull = "地址不能为空";
                }
            }

        }


        $scope.$emit("changeTopStyle", true);
    });